package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.EachLoveFanVo;
import com.tanhua.model.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author ck
 * @date 2021/11/11 13:20
 */

@RestController
@RequestMapping("/users")
@Slf4j
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户资料
     * 1、请求头：token
     * 2、请求参数： userID
     */
    @GetMapping
    public ResponseEntity users(@RequestHeader("Authorization") String token, Long userID) {
        //2、获取用户id
        Long id = UserHolder.getUserId();
        //3、判断用户id
        if (userID == null) {
            userID = Long.valueOf(id);
        }
        UserInfoVo userInfoVo = userInfoService.findById(userID);

        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestHeader("Authorization") String token,
                                         @RequestBody UserInfo userInfo) {
        //2、获取用户id
        Long id = UserHolder.getUserId();

        //3、设置用户id
        userInfo.setId(Long.valueOf(id));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 更新头像
     */

    @PostMapping("/header")
    public ResponseEntity header(@RequestHeader("Authorization") String token,
                                 MultipartFile headPhoto) throws IOException {
        //1、获取用户id
        Long userId = UserHolder.getUserId();
        //2、调用service完成头像更新
        userInfoService.updateHead(headPhoto, userId);
        return ResponseEntity.ok(null);
    }


    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        EachLoveFanVo vo = userInfoService.counts();
        return ResponseEntity.ok(vo);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity type(@PathVariable("type") String type,
                               @RequestParam("page") Integer page,
                               @RequestParam("pagesize") Integer pagesize,
                               @RequestParam(value = "nickname",required = false) String nickname) {

        PageResult pr = userInfoService.findList(type, page, pagesize, nickname);
        return ResponseEntity.ok(pr);
    }
}
